import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: '/',
    component: () => import('../views/home')
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/home.vue')
  },
  {
    path: '/baiDuMapGL',
    name: 'baiDuMapGL',
    meta:[ '原生百度地图' ],
    component: () => import('../views/百度地图/baiDuMapGL.vue')
  },
  {
    path: '/vue-baidu-map',
    name: 'vue-baidu-map',
    meta:[ 'vue-baidu-map地图组件' ],
    component: () => import('../views/百度地图/vue-baidu-map.vue')
  },
  {
    path: '/mapV',
    name: 'mapV',
    meta:[ 'mapV' ],
    component: () => import('../views/百度地图/mapV.vue')
  },
  {
    path: '/maskLayer',
    name: '遮罩层',
    meta:[ 'vue-baidu-map地图组件' ],
    component: () => import('../views/CSS知识/遮罩层.vue')
  },
  {
    path: '/textarea',
    name: '文本域',
    meta:[ '文本域' ],
    component: () => import('../views/CSS知识/文本域.vue')
  },
  // tabbar开始
  {
    path: '/tabbar-home',
    name: 'tabbar-home',
    meta:[ 'tabbar-home' ],
    component: () => import('../views/tabbar/home/home.vue')
  },
  {
    path: '/tabbar-category',
    name: 'tabbar-category',
    meta:[ 'tabbar-category' ],
    component: () => import('../views/tabbar/category/category.vue')
  },
  {
    path: '/tabbar-profile',
    name: 'tabbar-profile',
    meta:[ 'tabbar-profile' ],
    component: () => import('../views/tabbar/profile/profile.vue')
  },
  {
    path: '/tabbar-shopcart',
    name: 'tabbar-shopcart',
    meta:[ 'tabbar-shopcart' ],
    component: () => import('../views/tabbar/shopcart/shopcart.vue')
  },
  // tabbar结束
  // 动态路由开始
  {
    path: '/dynamicRouting',
    name: 'dynamic-routing',
    redirect:'/routerHome',
    // 路由懒加载
    component: () => import('../views/动态路由/index.vue'),
    children:[
      {
        path: '/routerHome',
        name: 'router-home',
        meta:{ title:'首页' },
        component: () => import('../views/动态路由/routerHome.vue'),
        children:[
          // 显示 children 里面的内容会自上而下默认展示第一个
          {
            path: '',
            redirect:'routerHomeNews'
          },
          // children 里面 path 不写 / 会默认拼接
          {
            path: 'routerHomeNews',
            name: 'router-home-news',
            meta:{ title:'新闻' },
            component: () => import('../views/动态路由/homeSon/news.vue')
          },
          {
            path: 'routerHomeMessage',
            name: 'router-home-message',
            meta:{ title:'消息' },
            component: () => import('../views/动态路由/homeSon/message.vue')
          },
        ]
      },
      {
        path: '/routerAbout',
        name: 'router-about',
        meta:{ title:'关于' },
        component: () => import('../views/动态路由/routerAbout.vue')
      },
      {
        path: '/routerUser/:userId',
        name: 'router-user',
        meta:{ title:'用户' },
        component: () => import('../views/动态路由/routerUser.vue')
      },
    ]
  },
  {
    path: '/promise',
    name: 'promise',
    meta:[ 'promise' ],
    component: () => import('../views/JS知识/promise.vue')
  },
  {
    path: '/VuexIndex',
    name: 'VuexIndex',
    meta:[ 'VueX' ],
    component: () => import('../views/VueX/index.vue')
  },
  {
    path: '/axios',
    name: 'axios',
    meta:[ 'axios' ],
    component: () => import('../views/axios/index.vue')
  },
  {
    path: '/echarts',
    name: 'echarts',
    meta:[ 'echarts' ],
    component: () => import('../views/Echarts/index.vue')
  },
  {
    path: '/popover',
    name: 'popover',
    meta:[ '二次封装弹出框' ],
    component: () => import('../views/el-popover/popover.vue')
  },
  {
    path: '/stqxzbScroll',
    name: 'stqxzbScroll',
    meta:[ '二次封装弹出框' ],
    component: () => import('../views/stqxzbScroll/index.vue')
  },
  {
    path: '/zjtx',
    name: 'zjtx',
    meta:[ 'vue的组件通信' ],
    component: () => import('../views/Vue组件通讯的方式/index.vue')
  },
  {
    path: '/zjfz',
    name: 'zjfz',
    meta:[ '组件封装' ],
    component: () => import('../views/组件封装/index.vue')
  },
  {
    path: '/smoothness',
    name: 'smoothness',
    meta:[ '组件封装' ],
    component: () => import('../views/平滑滚动锚点效果/index.vue')
  },
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

// 原因：查了一下说是因为路由重复（具体细节不太清楚）

// 解决办法：首先看自己是因为什么跳转方法报错：push/replace/back......

// 在router.js下写入下列两串代码（如果是其他跳转方法，把replace换成相应方法，例如replace换成push）
// 获取原型对象push函数
const originalPush = VueRouter.prototype.push

// 获取原型对象replace函数
const originalReplace = VueRouter.prototype.replace

// 修改原型对象中的push函数
VueRouter.prototype.push = function push(location){
  return originalPush.call(this , location).catch(err => err)
}

// 修改原型对象中的replace函数
VueRouter.prototype.replace = function replace(location){
  return originalReplace.call(this , location).catch(err => err)
}



router.beforeEach((to,from,next) => {
  // let loginData = localStorage.getItem('loginData');
  // if(to.path == '/login'){
  // document.title = to.meta.title
  next();
  // }
  // else if(loginData){
  //   next();
  // }else{
  //   next({ path:'/login' })
  // }
});
export default router
